package com.jqsoft.launcher.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <br/> <hr/>
 *
 * @author www.TheWk.cn.vc
 * @version 1.0 Jan 11, 2011
 * @class framework.utils.DateUtils
 */
public class DateTools {

	private static final DateFormat DF_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final DateFormat DF_YYYY_MM_DD_HH_MM_SS_XML = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	private static final DateFormat DF_YYYY_MM_DD_HH_MM = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static final DateFormat DF_YYYY_MM_DD       = new SimpleDateFormat("yyyy-MM-dd");
	private static final DateFormat DF_CN = new SimpleDateFormat("yyyy年MM月dd日"); 
	private static final DateFormat TIME_TAMP = new SimpleDateFormat("yyyyMMddHHmmssSSS");

	private DateTools() throws IllegalAccessException {
		throw new IllegalAccessException("工具类,不能实例化.");
	}

	/**
	 * 按月加,指定日期
	 *
	 * @param date
	 * @return
	 * @author www.TheWk.cn.vc
	 */
	public static Date getNextMonthByDate(Date date,int value) {
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, value);
		return calendar.getTime();
	}
	
	/**
	* 按年加,指定日期
	* 
	* @param date
	* @param value
	* @return
	*/
	public static Date getDateByAddYear(Date date, int value) {
	   Calendar calendar = Calendar.getInstance();
	   calendar.setTime(date);
	   calendar.add(Calendar.YEAR, value);
	   return calendar.getTime();
	}
	/**
	* 按日加,指定日期
	* 
	* @param date
	* @param value
	* @return
	*/
	public static Date getDateByAddDay(Date date, int value) {
	   Calendar calendar = Calendar.getInstance();
	   calendar.setTime(date);
	   calendar.add(Calendar.DATE, value);
	   return calendar.getTime();
	}
	
	
	/**
	 * 根据传入日期对比今天算出年龄
	 * @param date
	 * @return
	 * @author lgs
	 */
	public static Long getAgeByDate(Date date){
		  Date now=new Date();     
		  long day=(now.getTime()-date.getTime())/(24*60*60*1000) + 1;
		  return day/365;
	}


	public static String getFormatStringByDF_YYYY_MM_DD_HH_MM(Date date) {
		return DF_YYYY_MM_DD_HH_MM.format(date);
	}
	
	public static String getFormatStringByDF_YYYY_MM_DD_HH_MM_SS(Date date) {
		return DF_YYYY_MM_DD_HH_MM_SS.format(date);
	}
	
	public static String getFormatStringByDF_YYYY_MM_DD_HH_MM_SS_XML(Date date) {
		return DF_YYYY_MM_DD_HH_MM_SS_XML.format(date);
	}
	
	/**
	 * 返回17位时间戳
	 * @param date
	 * @return
	 */
	public static String getFormatStringTIME_TAMP(Date date)
	{
		return TIME_TAMP.format(date);
	}

	public static String getFormatStringByDF_YYYY_MM_DD(Date date) {
		return DF_YYYY_MM_DD.format(date);
	}
	public static String getFormatStringByDF_CN(Date date){
		return DF_CN.format(date);
	}
	
	/**
	 * 根据给定的开始时间和结束时间计算天数（不去除节假日）
	 * add by jiangbin
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getCountDays(Date begin,Date end)
	{
		int days = 0;
		try {
			long day = (end.getTime()-begin.getTime())/(24*60*60*1000);
			days = (int)day;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return days;
	}
	
	// 日期转换为中文格式
	private static final String[] NUMBERS = { "零", "一", "二", "三", "四", "五",
			"六", "七", "八", "九" };
	
	public static synchronized String toChinese(String str) {
		StringBuffer sb = new StringBuffer();
		sb.append(getSplitDateStr(str, 0)).append("年").append(
				getSplitDateStr(str, 1)).append("月").append(
				getSplitDateStr(str, 2)).append("日");
		return sb.toString();
	}
	
	public static String getSplitDateStr(String str, int unit) {
		// unit是单位 0=年 1=月 2日
		String[] DateStr = str.split("-");
		if (unit > DateStr.length)
			unit = 0;
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < DateStr[unit].length(); i++) {
	
			if ((unit == 1 || unit == 2) && Integer.valueOf(DateStr[unit]) > 9) {
				if(DateStr[unit].substring(0,1).equals("1")) {
					sb.append("十"+convertNum(DateStr[unit].substring(1, 2)));
				}else {
					sb.append(convertNum(DateStr[unit].substring(0, 1)))
	                	.append("十").append(
	                        convertNum(DateStr[unit].substring(1, 2)));
				}
				break;
			} else {
				sb.append(convertNum(DateStr[unit].substring(i, i + 1)));
			}
		}
		if (unit == 1 || unit == 2) {
			return sb.toString().replace("零", "");
		}
		return sb.toString();
	
	}
	
	private static String convertNum(String str) {
		return NUMBERS[Integer.valueOf(str)];
	}
}
